package Or

import (
	"database/sql"
	"fmt"
	"db2s/ETL"
	"db2s/global"
	"db2s/ref"
	"strings"
)

func (jm JobMe) Set(parameter string, value any) (err error) {
	return ref.UpdateStructJsonValue(jm, parameter, value)
}

func (jm JobMe) Result(object string) global.Return {
	switch object {
	default:
		return global.Return{}
	}
}
func (jm JobMe) Name(Con *sql.DB) (result global.Return, err error) {
	var event = "[Name]"
	var execSql []string
	jm.Parameter.Con = Con
	execSql = append(execSql, fmt.Sprintf("select JOB_NAME FROM DBA_SCHEDULER_JOBS where owner = '%s' %v", jm.Parameter.Object.Schema,
		func() string {
			switch {
			case jm.Parameter.Object.Table == "*":
				return ""
			case strings.Contains(jm.Parameter.Object.Table, "%"):
				return fmt.Sprintf(" AND JOB_NAME like '%%%v%%'", jm.Parameter.Object.Table)
			default:
				return fmt.Sprintf(" AND JOB_NAME = '%v'", jm.Parameter.Object.Table)
			}
		}()))
	execSql = append(execSql, fmt.Sprintf("select JOB FROM DBA_JOBS where 1=1 and SCHEMA_USER = '%v' %v", jm.Parameter.Object.Schema,
		func() string {
			switch {
			case jm.Parameter.Object.Table == "*":
				return ""
			case strings.Contains(jm.Parameter.Object.Table, "%"):
				return fmt.Sprintf(" AND JOB like '%v'", jm.Parameter.Object.Table)
			default:
				return fmt.Sprintf(" AND JOB = '%v'", jm.Parameter.Object.Table)
			}
		}()))
	if result, err = forExecQuerySql(event, jm.Parameter, ETL.StringGarth, execSql); err != nil || result.Result == nil {
		return
	}
	return
}
func (jm JobMe) dbaJobsExistence(Con *sql.DB) (result global.Return, err error) {
	var event = "[Existence]"
	var execSql []string
	jm.Parameter.Con = Con
	for _, v := range []string{"ALL_JOBS", "DBA_JOBS"} {
		execSql = append(execSql, fmt.Sprintf("select JOB from %v where 1 = 1 and SCHEMA_USER = '%v' and JOB = '%v'", v, jm.Parameter.Object.Schema, jm.Parameter.Object.Table))
	}
	if result, err = forExecQuerySql(event, jm.Parameter, ETL.String, execSql); err != nil || result.Result == nil {
		return
	}
	switch {
	case strings.EqualFold(fmt.Sprintf("%v", result.Result), jm.Parameter.Object.Table):
		result.Result = true
	default:
		result.Result = false
	}
	return
}
func (jm JobMe) dbaSchedulerExistence(Con *sql.DB) (result global.Return, err error) {
	var event = "[Existence]"
	var execSql []string
	jm.Parameter.Con = Con
	for _, v := range []string{"ALL_SCHEDULER_JOBS", "DBA_SCHEDULER_JOBS"} {
		execSql = append(execSql, fmt.Sprintf("select JOB_NAME from %v where 1 = 1 and owner = '%s'  and JOB_NAME = '%s'", v, jm.Parameter.Object.Schema, jm.Parameter.Object.Table))
	}
	if result, err = forExecQuerySql(event, jm.Parameter, ETL.String, execSql); err != nil || result.Result == nil {
		return
	}
	switch {
	case strings.EqualFold(fmt.Sprintf("%v", result.Result), jm.Parameter.Object.Table):
		result.Result = true
	default:
		result.Result = false
	}
	return
}
func (jm JobMe) Existence(Con *sql.DB) (result global.Return, err error) {
	if result, err = jm.dbaJobsExistence(Con); err == nil && result.Result != nil && result.Result.(bool) == true {
		return
	}
	if result, err = jm.dbaSchedulerExistence(Con); err != nil || result.Result != nil {
		return
	}
	return
}
func (jm JobMe) JobDefinition(Con *sql.DB) (result global.Return, err error) {
	return
}
